<?php
Namespace Api\Controller;
use Think\Controller;
use Api\Model;
class SearchApiController extends ApiBaseController{
    //获取默认用户名
    public function getDefaultUserName()
    {
        $strUsername = trim(I("username"));
        return $strUsername;
    }
    public function globalSearch(){
        $key=trim(I("key"));
        //验证token
        $token=trim(I('post.token'));
        $iParentID = $this->checkAndGetParentID($token);//这个函数可以把token解密出用户的id
        //根据查询条件 查找目标图书
        $where['BookName']=array('like','%'.$key.'%');
        $bookarr=MwithNoPrefix("TB_WxReadBook")->field('Id,BookName,DirName,CategoryIDList')->where($where)->select();
        $category=MwithNoPrefix("TB_WxReadCategory")->field('ID,CategoryName')->select();
        foreach($bookarr as $k=>$v){
            $bookarr[$k]['categoryidlist']=explode(',',$v['categoryidlist']);
            $bookarr[$k]['dirname']="http://osscdn.anniekids.net/weidiandu/".$v['dirname'].'/pic/fm.jpg';
        }
        foreach ($bookarr as $k=>$v){
            foreach($v['categoryidlist'] as $kk=>$vv){
                $book[$vv][]=$v;
            }
        }

        foreach ($category as $k=>$v){
            foreach ($book as $kk=>$vv){
                if($v['id']==$kk){
                    $new[$kk]['classifyName']=$v['categoryname'];
                    foreach ($book[$kk] as $kkk=>$vvv){
                        $new[$kk]['book'][]=array('bookName'=>$book[$kk][$kkk]['bookname'],
                            'bookImageUrl'=>$book[$kk][$kkk]['dirname'],
                            'bookId'=>$book[$kk][$kkk]['id']);
                    }
                }
            }
        }
        $arr=array_values($new);//把关联数组变成索引数组
        ShowJsonReturnAndDie($arr);
    }
    //未作功能的 暂时跳转这个页面
    public function index(){
        $this->display();
    }



}
